package com.lier.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author liyd
 * @description 全局跨域配置
 * @date 2025/6/7 15:53
 */
@Configuration
public class GateWayCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        // 1: 配置跨域
        // 允许任意请求头跨域
        corsConfiguration.addAllowedHeader("*");
        // 允许任意rest方法跨域
        corsConfiguration.addAllowedMethod("*");
        // 允许任意来源跨域
        corsConfiguration.addAllowedOrigin("*");
        // 是否允许携带cookie请求跨域
        corsConfiguration.setAllowCredentials(true);

        corsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsWebFilter(corsConfigurationSource);
    }
}
